package com.arajuuri.catinvaders;

import com.arajuuri.catinvaders.fileoperations.Images;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.Vector;
/**
 * <p>Title: Cat Invaders</p>
 * <p>Description: A space invaders clone with cats</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Semi Arajuuri
 * @version 1.0
 */

public class Human extends GraphicalObjectAdapter{
  private boolean newBullet=false;
  private int points=0;
  Vector bullets=new Vector();
  GameWindow parent;

  public Human(GameWindow gameWindow) {
    parent=gameWindow;
    img=images.loadImage("human.png");
    y=600-img.getHeight(null);
    startY=y;
    speed=10;
    hide();
  }

  public void move(){
    if(left){
     x-=speed;
     if(x<0){
       x=800;
     }
   }
   if(right){
     x+=speed;
     if(x>800){
       x=0;
     }
   }
  }

  public void resetBullets(){
    bullets=new Vector();
  }

  public int getPoints(){
    return points;
  }

  public void draw(Graphics2D g2d){
    if(bullets.size()<5 && newBullet){
      bullets.add(new Bullet(x, y));
    }
    newBullet=false;
    g2d.drawImage(img, x, y, null);
    Vector goVector = parent.getGraphicalObjects();
    Iterator bulletsIterator = bullets.iterator();
    while (bulletsIterator.hasNext()) {
      Bullet bullet = (Bullet) bulletsIterator.next();
      bullet.move();
      bullet.draw(g2d);
      if (bullet.detectCollision(goVector)) {
        points += 5;
      }
      if (bullet.isHidden()) {
        bulletsIterator.remove();
      }
    }
  }

  public void keyPressEvent(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
      left=true;
    }
    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
      right=true;
    }
    if(e.getKeyCode()==KeyEvent.VK_UP){
      up=true;
    }
    if(e.getKeyCode()==KeyEvent.VK_DOWN){
      down=true;
    }
    if(e.getKeyCode()==KeyEvent.VK_S){
      newBullet=true;
    }
    move();
  }

  public void keyReleaseEvent(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
      left=false;
    }
    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
      right=false;
    }
    if(e.getKeyCode()==KeyEvent.VK_UP){
      up=false;
    }
    if(e.getKeyCode()==KeyEvent.VK_DOWN){
      down=false;
    }
    move();
  }
}
